package cn.vplay.controllers;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import cn.vplay.beans.Category;
import cn.vplay.beans.Movie;
import cn.vplay.beans.User;
import cn.vplay.services.CategoryService;
import cn.vplay.services.MovieService;
import cn.vplay.utils.SaveFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class MovieController {
	@Autowired
	private  MovieService movieService;

	@Autowired
	private CategoryService categoryService;

	@RequestMapping("/showIndex")   //导航界面跳转到主界面indexs,并获得各个列表
	public String showIndex(HttpServletRequest request) {

		List<Movie> sliders = this.movieService.getMovieBySliderImage();
		List<Movie> movies = this.movieService.getMovieByCategory(1l);
		List<Movie> tvplays = this.movieService.getMovieByCategory(2l);;
		List<Movie> rankings = this.movieService.selectByRankings();

		request.setAttribute("sliders", sliders);
		request.setAttribute("movies", movies);
		request.setAttribute("tvplays", tvplays);
		request.setAttribute("rankings", rankings);

		return "indexs";
	}

	@RequestMapping("/getMovieByCategory")   //顶部跳转功能
	public String getMovieByCategory(@RequestParam(value="category") Long category,
									 Movie movie, HttpServletRequest request) {

		movie.setCategoryId(category);

		List<Movie> movies = this.movieService.getMoviesByUserID(movie);
		List<Movie> rankings = this.movieService.selectByRankingsByCategory(category);
		request.setAttribute("movies", movies);
		request.setAttribute("rankings", rankings);
		if(category==1) {
			return "movie/movie";
		}else if(category==2) {
			return "movie/TVPlay";
		}else if(category==3) {
			return "movie/comic";
		}else if(category==4) {
			return "movie/variety";
		}else {
			return "movie/music";
		}
	}

	@RequestMapping("/getMoviesByUserID")   //去个人中心影片管理界面
	public String getMoviesByUserID(Movie movie,HttpSession session,HttpServletRequest request) {
		User user = (User) session.getAttribute("user");
		movie.setUserId(user.getId());
		List<Movie> movielist = this.movieService.getMoviesByUserID(movie);
		request.setAttribute("movielist", movielist);
		return "user/filmManage";
	}

	@RequestMapping("/searchIndexMovie")                 //首页的搜索
	public String searcIndexhMovie(Movie movie,HttpServletRequest request) {
		String condiction = movie.getMovieName();
		movie.setActors(condiction);
		movie.setDirector(condiction);
		movie.setMessage(condiction);
		movie.setMovieCountry(condiction);
		List<Movie> listMovie = this.movieService.searchMovie(movie);
		request.setAttribute("listMovie", listMovie);
		List<Movie> rankings = this.movieService.selectByRankings();
		request.setAttribute("rankings", rankings);
		request.setAttribute("search", condiction);

		return "movie/search";
	}

	@RequestMapping("/searchMovie")             //个人中心搜索页
	public String searchMovie(Movie movie,HttpServletRequest request) {
		String condiction = request.getParameter("movieName");
		User user = (User) request.getSession().getAttribute("user");
		movie.setActors(condiction);
		movie.setDirector(condiction);
		movie.setMessage(condiction);
		movie.setMovieName(condiction);
		movie.setMovieCountry(condiction);
		movie.setUserId(user.getId());
		List<Movie> listMovie = this.movieService.searchMovie(movie);
		request.setAttribute("listMovie", listMovie);
		List<Movie> rankings = this.movieService.selectByRankings();
		request.setAttribute("rankings", rankings);
		request.setAttribute("search", condiction);
		return "user/search";
	}


	@RequestMapping("/toUpload")  //去用户上传影片界面
	public String toUpload(HttpServletRequest request) {
		List<Category> categories = this.categoryService.getCategorys();
		if(null!=categories) {
			request.setAttribute("categories", categories);
		}
		return "upload";
	}


	@RequestMapping("/updateMovie")               //上传电影
	public String updateMovie(Movie movie, HttpSession session, HttpServletRequest request,
							  @RequestParam(value = "moviePicture",required=false) MultipartFile moviePicture) {

		String uuid = (String) session.getAttribute("uuid");
		String exMv = (String) session.getAttribute("exMv");
		movie.setId(uuid);
		movie.setMvType("."+exMv);
		User user = (User) session.getAttribute("user");
		user.getUserName();
		movie.setUserId(user.getId());
		if(!moviePicture.isEmpty()) {
			SaveFile.saveMvPicture(user,moviePicture,movie);
		}else {
			SaveFile.saveDefaultMVPicture(user,movie,request);
		}
		int result = this.movieService.insertSelective(movie); //添加电影及信息，添加成功result = 1
		if(result>0) {
			return "redirect:toUpload";
		}else {
			return "redirect:toUpload";
		}
	}


	@RequestMapping("/player")      //播放界面
	public String playMovie(HttpSession session,HttpServletRequest request,
							@RequestParam(value = "movie",required=false) String movie) {

		String[] ids = movie.split("\\.");
		String movieId = ids[0];
		Movie movieResult = this.movieService.getMoviesByID(movieId);
		Movie moviess = new Movie();
		moviess.setId(movieResult.getId());
		moviess.setPlays(movieResult.getPlays()+1);
		this.movieService.updateByCondiction(moviess);
		if(null!=movieResult.getAdminId()) {
			String admin = "admin";
			request.setAttribute("userName", admin);
		}else {
			User user = (User) session.getAttribute("user");
			request.setAttribute("userName", user.getUserName());
		}
		request.setAttribute("movieResult", movieResult);
		if(movieResult.getCategoryId()!=1) {
			String mvs = movieId.substring(0, movieId.length()-2);
			String mv = movieId.substring(movieId.length()-1, movieId.length());
			int i = Integer.valueOf(mv);
			if(i==1) {
				request.setAttribute("i", 2);
			}else if(i==5){
				request.setAttribute("i", 4);
			}else {
				request.setAttribute("i", i);
			}

			List<Movie> movies = this.movieService.getMoviesByIDs(mvs);
			request.setAttribute("movies", movies);
		}else {
			request.setAttribute("i", 2);
			List<Movie> movies = new ArrayList<Movie>();
			movies.add(movieResult);
			request.setAttribute("movies", movies);
		}
		return "player";
	}


}
